package com.solotiger.okex.config;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.solotiger.okex.domain.okex.FutureUsdChannel;
import com.solotiger.okex.service.IOkexService;
import com.solotiger.okex.websocket.WebSocketService;
import com.solotiger.okex.websocket.impl.OkexWebSocketClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.List;


@Component
public class OkexProcessor implements DisposableBean, WebSocketService {

    private OkexWebSocketClient client;
    private OkexWebSocketClient spotClient;

    private Gson gson = new Gson();
    private DecimalFormat df = new DecimalFormat("#.00");

    private double btcBuy = -1d; //btc合约买一
    private double btcSell = -1d; //btc现货卖一

    @Autowired
    private IOkexService okexService;

    public OkexProcessor() {


    }

    public void start(){
        client = new OkexWebSocketClient("wss://real.okex.com:10440/websocket/okexapi", this);
        spotClient = new OkexWebSocketClient("wss://real.okex.com:10441/websocket", this);

        client.start();
        spotClient.start();

        addChannel("btc");

    }

//    public static void main(String[] args) {
////        OkexWebSocketClient client = new OkexWebSocketClient("wss://real.okex.com:10440/websocket/okexapi", new WebSocketServiceImpl());
////        client.start();
////        client.addChannel("ok_sub_futureusd_btc_ticker_this_week");
//
//        new OkexProcessor();
//    }

    @Override
    public void onReceive(String msg) {
//        FutureUserInfoResult reuslt = okexService.futureUserInfo(UrlContants.API_KEY, UrlContants.secretKey);
//        System.out.println("soltiger" + reuslt.isResult());
//        System.out.println("result = " + reuslt.getInfo().get("btc").getRisk_rate());
        if ("{\"event\":\"pong\"}".equals(msg)) {
            return;
        }
        try {
            //目前只用户sell和buy字段,所以统一用FutureTicker,后续视需求而定
            List<FutureUsdChannel> list = gson.fromJson(msg, new TypeToken<List<FutureUsdChannel>>() {
            }.getType());
            for (FutureUsdChannel channel : list) {
                if ("ok_sub_futureusd_btc_ticker_this_quarter".equals(channel.getChannel())) {
                    btcBuy = channel.getData().getBuy();
                    String result = sub(btcBuy, btcSell);
                    if (result != null) {
                        double d = Double.parseDouble(result);
                        System.out.println("ok_sub_futureusd_btc_ticker_this_quarter = " + d);
                    }
                } else if ("ok_sub_spot_btc_usdt_ticker".equals(channel.getChannel())) {
                    btcSell = channel.getData().getSell();
                    String result = sub(btcBuy, btcSell);
                    if (result != null) {
                        double d = Double.parseDouble(result);
                        System.out.println("ok_sub_spot_btc_ticker = " + d);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private String sub(double buy, double sell) {
        if (buy != -1 && sell != -1) {
            double s = ((buy - sell) / sell);
            return df.format(s * 100);
        }
        return null;
    }

    @Override
    public void destroy() throws Exception {
        stop();
    }

    public void stop(){
        if(client != null){
            client.clear();
            client = null;
        }
        if(spotClient != null){
            spotClient.clear();
            spotClient = null;
        }
    }

    public boolean getStatus(){
        if(client == null || spotClient == null){
            return false;
        }
        return client.getStatus() && spotClient.getStatus();
    }

    public void addChannel(String ticker) {
        client.addChannel("ok_sub_futureusd_" + ticker + "_ticker_this_quarter");
        spotClient.addChannel("ok_sub_spot_" + ticker + "_usdt_ticker");
    }
}
